
export class MjWeaveItem {

    public static Weave_Type = {
        Null: 0, //
        Peng: 1, //碰
        Gang: 2, //杠
        AnGang: 3, //暗杠
        Bian: 4, //边
        Za: 5, //砸
        GangZa: 6, //杠砸
        AnGangZa: 7, //暗杠砸
        Zuan: 8, //钻
    }

    public weaveType: number; //组合类型     
    public operateChair: number = 0 //操作用户
    public provideChair: number; //供应用户
    public provideCard: number; //供应牌
    public cardList: Array<number> = []


    public static test_mjWeaveList: Array<Array<MjWeaveItem>> = [
        [
            // new MjWeaveItem(MjWeaveItem.Weave_Type.Peng, 0, 0x12, [0x12, 0x12, 0x12]),
    
        ],
        [
            // new MjWeaveItem(MjWeaveItem.Weave_Type.Zuan, 0, 0, 0x13, [0x12, 0x14, 0x13]),
        ],
        [
            // new MjWeaveItem(MjWeaveItem.Weave_Type.Zuan, 0, 0, 0x13, [0x12, 0x14, 0x13]),
        ],
        [
            // new MjWeaveItem(MjWeaveItem.Weave_Type.Zuan, 0, 0, 0x13, [0x12, 0x14, 0x13]),
        ]
    ]



    constructor(weaveType: number = 0, operateChair: number, provideChair: number = 0, provideCard: number = 0, cardList: Array<number>) {
        this.weaveType = weaveType;
        this.operateChair = operateChair;
        this.provideChair = provideChair;
        this.provideCard = provideCard;
        this.cardList = cardList;
    }

    public static json2MjWeaveItemList(dataList: Array<Array<object>>) {
        let resultList: Array<Array<MjWeaveItem>> = []
        for (let i = 0; i < dataList.length; i++) {
            resultList[i] = []
            for (let j = 0; j < dataList[i].length; j++) {
                let dataObj: object = dataList[i][j]
                resultList[i][j] = this.json2MjWeaveItem(dataObj)
            }
        }
        return resultList;
    }

    public static json2MjWeaveItem(dataObj: object) {
        let mjWeaveItem: MjWeaveItem
        if (dataObj) {
            mjWeaveItem = new MjWeaveItem(dataObj["weaveType"], dataObj["operateChair"], dataObj["provideChair"], dataObj["provideCard"], dataObj["cardList"])
        }
        return mjWeaveItem
    }

    public static addHandCardByWeaveItem(mjWeaveItem: MjWeaveItem, handCardList: Array<number>){
        let deleteCardList: Array<number> = mjWeaveItem.cardList.concat()
        // if (mjWeaveItem.weaveType == MjWeaveItem.Weave_Type.Gang || mjWeaveItem.weaveType == MjWeaveItem.Weave_Type.AnGang
        //     || mjWeaveItem.weaveType == MjWeaveItem.Weave_Type.GangZa || mjWeaveItem.weaveType == MjWeaveItem.Weave_Type.AnGangZa) {
        //     // deleteCardList.push(mjWeaveItem.provideCard)

        // }
        // handCardList = handCardList.concat(deleteCardList)
        if(mjWeaveItem.weaveType == MjWeaveItem.Weave_Type.AnGang || mjWeaveItem.weaveType == MjWeaveItem.Weave_Type.AnGangZa){
            handCardList.push(mjWeaveItem.cardList[0])
            handCardList.push(mjWeaveItem.cardList[0])
            handCardList.push(mjWeaveItem.cardList[0])
            handCardList.push(mjWeaveItem.cardList[0])
        }
        else if(mjWeaveItem.weaveType == MjWeaveItem.Weave_Type.Bian || mjWeaveItem.weaveType == MjWeaveItem.Weave_Type.Gang
            || mjWeaveItem.weaveType == MjWeaveItem.Weave_Type.GangZa || mjWeaveItem.weaveType == MjWeaveItem.Weave_Type.Zuan){
            handCardList.push(mjWeaveItem.cardList[0])
            handCardList.push(mjWeaveItem.cardList[1])
            handCardList.push(mjWeaveItem.cardList[2])
        }
        else if(mjWeaveItem.weaveType == MjWeaveItem.Weave_Type.Peng || mjWeaveItem.weaveType == MjWeaveItem.Weave_Type.Za){
            handCardList.push(mjWeaveItem.cardList[0])
            handCardList.push(mjWeaveItem.cardList[1])
        }


    }

    public static deleteHandCardByWeaveItem(mjWeaveItem: MjWeaveItem, handCardList: Array<number>) {
        let deleteCardList: Array<number> = mjWeaveItem.cardList.concat()
        if (mjWeaveItem.weaveType == MjWeaveItem.Weave_Type.Gang || mjWeaveItem.weaveType == MjWeaveItem.Weave_Type.AnGang
            || mjWeaveItem.weaveType == MjWeaveItem.Weave_Type.GangZa || mjWeaveItem.weaveType == MjWeaveItem.Weave_Type.AnGangZa) {
            deleteCardList.push(mjWeaveItem.provideCard)
        }
        if(mjWeaveItem.weaveType == MjWeaveItem.Weave_Type.Peng
            || mjWeaveItem.weaveType == MjWeaveItem.Weave_Type.Za){
            deleteCardList = deleteCardList.splice(0, 2)
        }
        for (let i = 0; i < deleteCardList.length; i++) {
            for (let j = handCardList.length - 1; j >= 0; j--) {
                if (handCardList[j] == deleteCardList[i]) {
                    handCardList.splice(j, 1)
                    break;
                }
            }
        }
    }

}